package day6;

import java.io.File;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class ReverseFile {
    public static void reverse(File f) {
        LinkedList<File> files = new LinkedList<>();
        files.add(f);
        //System.out.println(f.getAbsolutePath());
        while (files.size() > 0) {
            File temp = files.removeFirst();
            System.out.println(temp.getAbsolutePath());
            File[] fr = temp.listFiles();
            if (fr == null)
                continue;
            for (File t : fr) {
                System.out.println("t:" + t);
                files.add(t);
            }
        }
    }

    public static void reverseSize(File f) {
        LinkedList<File> files = new LinkedList<>();
        files.add(f);

        while (files.size() > 0) {
            File temp = files.removeFirst();
            System.out.println(temp.getAbsolutePath());
            File[] fr = temp.listFiles();
            if (fr == null)
                continue;
            for (File t : fr) {
                System.out.println("t:" + t);
                files.add(t);
            }
        }
    }
    //文件夹大小
    public static long getSize(File f) {
        if (f.isFile())
            return f.length();
        long total=0;
        File []fr=f.listFiles();
        for(File r:fr)
        {
            if(r.isFile())
                total+=r.length();
            else
                total+=getSize(r);
        }
        return total;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入文件路径：");
        String path = in.nextLine();
        //reverse(new File(path));
        System.out.println(getSize(new File(path)));
    }
}
